package org.dtrd.modules.evaluation.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.ArrayUtils;
import org.dtrd.modules.evaluation.entity.bean.DiseaseEvaluationInfo;
import org.dtrd.modules.evaluation.entity.dto.DiseaseDTO;
import org.dtrd.modules.evaluation.entity.po.DtrdEntEvaluationRdNeuropathy;
import org.dtrd.modules.evaluation.mapper.DtrdEntEvaluationRdNeuropathyMapper;
import org.dtrd.modules.evaluation.service.IDtrdEntEvaluationRdNeuropathyService;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 * 神经病变评估 服务实现类
 * </p>
 *
 * @author QR
 * @since 2021-09-26
 */
@Service
@DS("multi-datasource1")
public class DtrdEntEvaluationRdNeuropathyServiceImpl extends ServiceImpl<DtrdEntEvaluationRdNeuropathyMapper, DtrdEntEvaluationRdNeuropathy> implements IDtrdEntEvaluationRdNeuropathyService {

    @Override
    public DiseaseEvaluationInfo saveNeuropathyInfo(DiseaseDTO dto) {
        // 分类或分级诊断
        Map<Integer, String> diagnoseResultMap = new HashMap<>();
        diagnoseResultMap.put(1, "不排除糖尿病神经病变");
        diagnoseResultMap.put(2, "不排除糖尿病周围神经病变");
        diagnoseResultMap.put(3, "不排除消化系统自主神经病变");
        diagnoseResultMap.put(4, "不排除泌尿生殖系统自主神经病变");
        diagnoseResultMap.put(5, "不排除心血管自主神经病变");
        // 建议复诊时间
        Map<Integer, String> revisitTimeMap = new HashMap<>();
        revisitTimeMap.put(1, "建议及时就诊");
        revisitTimeMap.put(2, "每隔3-6月进行复查");
        revisitTimeMap.put(3, "每年至少筛查一次");
        // 就诊科室
        Map<Integer, String> departmentMap = new HashMap<>();
        departmentMap.put(1, "内分泌科就诊");
        departmentMap.put(3, "必要时消化内科就诊");
        departmentMap.put(4, "必要时泌尿外科就诊");
        departmentMap.put(5, "必要时心内科就诊");
        departmentMap.put(6, "必要时皮肤科就诊");
        // 具体检查内容推荐
        Map<Integer, String> examinationProjectMap = new HashMap<>();
        examinationProjectMap.put(1, "神经传导功能检查");
        examinationProjectMap.put(2, "体格检查：<br>" + "    1）针刺痛觉<br>" + "    2）温度觉<br>" + "    3）压力觉<br>" + "    4）振动觉<br>" + "    5）踝反射");
        examinationProjectMap.put(3, "胃电图");
        examinationProjectMap.put(4, "食管测压");
        examinationProjectMap.put(5, "胃排空闪烁图扫面");
        examinationProjectMap.put(6, "直肠局部末梢神经病变电生理检查");
        examinationProjectMap.put(7, "膀胱超声：膀胱容量、残余尿量");
        examinationProjectMap.put(8, "神经传导速度检查");
        examinationProjectMap.put(9, "心率变异性检查");
        examinationProjectMap.put(10, "Valasalva试验（最长R-R间期与最短R-R间期之比）");
        examinationProjectMap.put(11, "握拳试验");
        examinationProjectMap.put(12, "体位性低血压变化测定");
        examinationProjectMap.put(13, "24h动态血压监测");
        // 是否确诊：1-未确诊,2-已确诊,3-确诊无
        int isDiagnose = dto.getIsDiagnose();
        // 症状或检查
        Integer[] symptomArr = dto.getSymptom();
        StringJoiner joiner = new StringJoiner(",");
        for (Integer i : symptomArr) {
            joiner.add(i + "");
        }
        Set<Integer> diagnoseResultSet = new HashSet<>();
        Set<Integer> revisitTimeSet = new HashSet<>();
        Set<Integer> departmentSet = new HashSet<>();
        Set<Integer> examinationProjectSet = new HashSet<>();
        if (1 == isDiagnose) {
            if (ArrayUtils.contains(symptomArr, 1) || ArrayUtils.contains(symptomArr, 25)) {
                diagnoseResultSet.add(1);
                revisitTimeSet.add(1);
                departmentSet.add(1);
                examinationProjectSet.add(1);
                examinationProjectSet.add(2);
            }
            if (ArrayUtils.contains(symptomArr, 2) || ArrayUtils.contains(symptomArr, 3) || ArrayUtils.contains(symptomArr, 4)
                    || ArrayUtils.contains(symptomArr, 5) || ArrayUtils.contains(symptomArr, 6) || ArrayUtils.contains(symptomArr, 7)
                    || ArrayUtils.contains(symptomArr, 8)) {
                diagnoseResultSet.add(2);
                revisitTimeSet.add(1);
                departmentSet.add(1);
                examinationProjectSet.add(1);
                examinationProjectSet.add(2);
            }
            if (ArrayUtils.contains(symptomArr, 9) || ArrayUtils.contains(symptomArr, 10) || ArrayUtils.contains(symptomArr, 11)
                    || ArrayUtils.contains(symptomArr, 12) || ArrayUtils.contains(symptomArr, 13) || ArrayUtils.contains(symptomArr, 14)
                    || ArrayUtils.contains(symptomArr, 15)) {
                diagnoseResultSet.add(3);
                revisitTimeSet.add(1);
                departmentSet.add(1);
                departmentSet.add(3);
                examinationProjectSet.add(3);
                examinationProjectSet.add(4);
                examinationProjectSet.add(5);
                examinationProjectSet.add(6);
            }
            if (ArrayUtils.contains(symptomArr, 16) || ArrayUtils.contains(symptomArr, 17) || ArrayUtils.contains(symptomArr, 18)) {
                diagnoseResultSet.add(4);
                revisitTimeSet.add(1);
                departmentSet.add(1);
                departmentSet.add(4);
                examinationProjectSet.add(7);
                examinationProjectSet.add(8);
            }
            if (ArrayUtils.contains(symptomArr, 19) || ArrayUtils.contains(symptomArr, 20) || ArrayUtils.contains(symptomArr, 21)
                    || ArrayUtils.contains(symptomArr, 22)) {
                diagnoseResultSet.add(5);
                revisitTimeSet.add(1);
                departmentSet.add(1);
                departmentSet.add(5);
                examinationProjectSet.add(9);
                examinationProjectSet.add(10);
                examinationProjectSet.add(11);
                examinationProjectSet.add(12);
                examinationProjectSet.add(13);
            }
            if (ArrayUtils.contains(symptomArr, 23) || ArrayUtils.contains(symptomArr, 24)) {
                diagnoseResultSet.add(1);
                revisitTimeSet.add(1);
                departmentSet.add(1);
                departmentSet.add(6);
            }
        } else if (2 == isDiagnose) {
            if (ArrayUtils.contains(symptomArr, 1) && ArrayUtils.contains(symptomArr, 3)) {
                revisitTimeSet.add(3);
                examinationProjectSet.add(1);
                examinationProjectSet.add(2);
            } else if (ArrayUtils.contains(symptomArr, 2) || ArrayUtils.contains(symptomArr, 4)) {
                revisitTimeSet.add(2);
                examinationProjectSet.add(1);
                examinationProjectSet.add(2);
            }
        } else if (3 == isDiagnose) {
            if (ArrayUtils.contains(symptomArr, 1) && ArrayUtils.contains(symptomArr, 3)) {
                revisitTimeSet.add(3);
                examinationProjectSet.add(1);
                examinationProjectSet.add(2);
            } else if (ArrayUtils.contains(symptomArr, 2) || ArrayUtils.contains(symptomArr, 4)) {
                revisitTimeSet.add(2);
                examinationProjectSet.add(1);
                examinationProjectSet.add(2);
            }
        }
        StringBuilder diagnoseResult = new StringBuilder();
        StringBuilder revisitTime = new StringBuilder();
        StringBuilder department = new StringBuilder();
        StringBuilder examinationProject = new StringBuilder();
        for (int diagnoseResultKey : diagnoseResultSet) {
            diagnoseResult.append(diagnoseResultMap.get(diagnoseResultKey)).append("、");
        }
        diagnoseResult = new StringBuilder("".equals(diagnoseResult.toString()) ? "" : diagnoseResult.substring(0, diagnoseResult.length() - 1));
        for (int revisitTimeKey : revisitTimeSet) {
            revisitTime.append(revisitTimeMap.get(revisitTimeKey)).append("，");
        }
        revisitTime = new StringBuilder("".equals(revisitTime.toString()) ? "" : revisitTime.substring(0, revisitTime.length() - 1));
        for (int departmentKey : departmentSet) {
            department.append(departmentMap.get(departmentKey)).append("，");
        }
        department = new StringBuilder("".equals(department.toString()) ? "" : department.substring(0, department.length() - 1));
        int examinationProjectIndex = 0;
        for (int examinationProjectKey : examinationProjectSet) {
            examinationProject.append(++examinationProjectIndex).append("、").append(examinationProjectMap.get(examinationProjectKey)).append("<br>");
        }
        examinationProject = new StringBuilder("".equals(examinationProject.toString()) ? "" : examinationProject.substring(0, examinationProject.length() - 4));

        // 写入数据库
        DtrdEntEvaluationRdNeuropathy neuropathy = new DtrdEntEvaluationRdNeuropathy();
        neuropathy.setCreateTime(new Date());
        neuropathy.setIsDiagnose(isDiagnose);
        neuropathy.setSymptom(joiner.toString());
        neuropathy.setDiagnoseResult(diagnoseResult.toString());
        neuropathy.setRevisitTime(revisitTime.toString());
        neuropathy.setDepartment(department.toString());
        neuropathy.setExaminationProject(examinationProject.toString());
        neuropathy.setOutput(diagnoseResult + "<br>" + revisitTime + "<br>" + department + "<br>" + examinationProject);
        if (neuropathy.getPatientId() != null) {
            neuropathy.setPatientId(dto.getPatientId());
            neuropathy.setUserType(1);
            save(neuropathy);
        }
        return new DiseaseEvaluationInfo().parseFromPo(neuropathy);
    }

    @Override
    public DiseaseEvaluationInfo getNeuropathyInfoLatest(Integer patientId) {
        LambdaQueryWrapper<DtrdEntEvaluationRdNeuropathy> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper
                .eq(DtrdEntEvaluationRdNeuropathy::getPatientId, patientId)
                .eq(DtrdEntEvaluationRdNeuropathy::getIsDel, 0)
                .orderByDesc(DtrdEntEvaluationRdNeuropathy::getCreateTime)
                .last("limit 1");
        DtrdEntEvaluationRdNeuropathy neuropathy = baseMapper.selectOne(lambdaQueryWrapper);
        DiseaseEvaluationInfo info = new DiseaseEvaluationInfo();
        if (neuropathy != null) {
            info = info.parseFromPo(neuropathy);
        }
        return info;
    }
}
